package com.office.sysManage.staffManage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.office.sysManage.staffManage.dao.StaffMapper;
import com.office.sysManage.staffManage.pojo.Position;
import com.office.sysManage.staffManage.pojo.Staff;
import com.office.sysManage.staffManage.service.StaffService;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/01/01
 * 星期六 15:27
 */
@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private StaffMapper staffMapper;

    public StaffMapper getStaffMapper() {
        return staffMapper;
    }
    public void setStaffMapper(StaffMapper staffMapper) {
        this.staffMapper = staffMapper;
    }

    /**
     * 根据条件查询员工信息
     * @param staff 员工对象
     * @return 员工列表
     */
    @Override
    public PageModel<Staff> queryStaffByCondition(Staff staff, PageModel<Staff> pageModel) {
        if (staff != null){
            if ("%".equals(staff.getName()) || "_".equals(staff.getName())){
                staff.setName(null);
            }
            pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
            pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
            PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());

            List<Staff> staffList = staffMapper.queryStaffByCondition(staff);

            PageInfo<Staff> pageInfo = new PageInfo<Staff>(staffList);

            pageModel.setData(new ArrayList<Staff>(staffList));
            pageModel.setTotalPage(pageInfo.getPages());
            pageModel.setTotalRows(pageInfo.getTotal());
            pageModel.setNavigatepageNums(pageInfo.getNavigatepageNums());
            pageModel.setFirstPage(pageInfo.isIsFirstPage());
            pageModel.setLastPage(pageInfo.isIsLastPage());

            return pageModel;
        }
        return null;
    }

    /**
     * 查询员工信息
     * @param staff 员工对象
     * @return 员工对象
     */
    @Override
    public Staff queryStaffByCondition(Staff staff) {
        return staffMapper.queryStaffInfo(staff);
    }

    /**
     * 添加员工信息
     * @param staff 员工对象
     * @return 是否添加成功
     */
    @Override
    public boolean addStaff(Staff staff) {
        if (staff != null){
            // 创建员工编号
            int i = staffMapper.queryLastId();
            String newStaffId = "oa" + (i+1);
            staff.setStaffId(newStaffId);
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            String psw = passwordEncoder.encode(staff.getPassword());
            staff.setPassword(psw);
            int positionId = staffMapper.queryPositionByName(staff);
            staff.setPositionId(positionId);
            return staffMapper.addStaff(staff) >= 1;
        }
        return false;
    }

    /**
     * 逻辑删除员工信息
     * @param staff 员工对象
     * @return 是否删除成功
     */
    @Override
    public boolean deleteStaff(Staff staff) {
        if (staff != null){
            return staffMapper.deleteStaff(staff) >= 1;
        }
        return false;
    }

    /**
     * 查询所有的职位
     * @return 职位列表
     */
    @Override
    public List<Position> queryPositionList() {
        return staffMapper.queryPositionList();
    }

    /**
     * 修改员工信息
     * @param staff 修改的员工对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateStaff(Staff staff) {
        if (staff != null){
            // 设置职位ID
            int position = staffMapper.queryPositionByName(staff);
            staff.setPositionId(position);
            return staffMapper.updateStaff(staff) > 0;
        }
        return false;
    }

}
